<!DOCTYPE html>

<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="user-scalable=no, width=device-width"/>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile-1.4.4.min.css">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile.structure-1.4.4.min.css">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile.inline-png-1.4.4.min.css">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile.inline-svg-1.4.4.min.css">
        <link rel="stylesheet" href="css/jquery.mobile-1.4.4/jquery.mobile.external-png-1.4.4.min.css">
        <!-- Choix du thème -->
        <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css">
        <link rel="stylesheet" href="css/bootstrap/jquery.mobile.icons.min.css">
        <!-- JavaScript -->
        <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.4.4.min.js"></script>
        <script type="text/javascript" src="js/datebox/jqm-datebox.core.js"></script>
        <script type="text/javascript" src="js/datebox/jqm-datebox.mode.datebox.js"></script>
        <title></title>
    </head>
    <body>
        <?php
                require_once '../controller/TacheManager.php';
                require_once '../controller/TypeTacheManager.php';
                include_once '../model/config.php';
                
        ?>
        
        <?php if ( filter_input(INPUT_GET,'id') ) : ?>
            <?php
                     session_start();
                     $manager = new TacheManager($bdd);
                     $manager2 = new TypeTacheManager($bdd);
                     $tache = $manager->getTachebyId(filter_input(INPUT_GET,'id'));
                     $idProjet = filter_input(INPUT_GET,'idPro');
                     $listType = $manager2->getListTypeTache($_SESSION["categorie"]);
            ?>
        
            <?php if ( filter_input(INPUT_GET,'act') == "r" ): ?>
                <div data-role="page">
                    <?php 
                          // voir la tâche 
                          $prevpage = filter_input(INPUT_GET,'hd');$idProjet = filter_input(INPUT_GET,'idPro');
                          include 'includes/tache.php'; 
                          include 'includes/footer.php'; 
                    ?>
                </div>
            <?php else: ?>
                    <?php 
                        session_start();
                        $manager->deleTache(filter_input(INPUT_GET,'id'),$_SESSION["categorie"]);
                        
                        if ( filter_input(INPUT_GET,'rtpage') == "Rappels" ){
                               header("Location:Tache.php?idProjet=0"); 
                        }else{

                               if ( $_SESSION["categorie"] == 1){
                                   header("Location:Tache.php?idProjet=".filter_input(INPUT_GET,'idPro')); 
                               }else if( $_SESSION["categorie"] == 2 ) {
                                   header("Location:Tache.php?idExamen=".filter_input(INPUT_GET,'idPro')); 
                               }else if( $_SESSION["categorie"] == 3 ) {
                                   header("Location:Tache.php?idStage=".filter_input(INPUT_GET,'idPro')); 
                               }
                               
                        }
                    ?>
            <?php endif ?>
                
        <?php else: ?>
            <?php if ( filter_input(INPUT_GET,'act') == "c" ): ?>
        
                <?php  
                    $manager = new TacheManager($bdd); 
                ?>
        
                <?php if ( filter_input(INPUT_GET,'task') ): ?>
                       <?php 
                            // Enregister la tache en bdd 
                            session_start();
                            $tache = json_decode(filter_input(INPUT_GET,'task'));
                            $manager = new TacheManager($bdd);
                            if ( $tache->id == 0){
                                $manager->creerTache($tache,$_SESSION["id"],$_SESSION["categorie"]);
                            }else {
                                $manager->UpdateTache($tache);
                            }
                            if ( filter_input(INPUT_GET,'rtpage') == "Rappels" ){
                               header("Location:Tache.php?idProjet=0"); 
                            }else{
                               //rtpage=TacheProjet
                               if ( $_SESSION["categorie"] == 1){
                                   header("Location:Tache.php?idProjet=".$tache->idprojet);
                               }else if( $_SESSION["categorie"] == 2 ) {
                                   header("Location:Tache.php?idExamen=".$tache->idprojet);
                               }else if( $_SESSION["categorie"] == 3 ) {
                                   header("Location:Tache.php?idStage=".$tache->idprojet);
                               }  
                            }
                       ?>
        
                <?php else: ?>
                        <div data-role="page" >
                            <?php 
                                   // Créer la tache
                                   session_start();
                                   $manager2 = new TypeTacheManager($bdd);
                                   $listType = $manager2->getListTypeTache($_SESSION["categorie"]);
                                   $tache = $manager->defaultTache($_SESSION["categorie"]);
                                   $idProjet = filter_input(INPUT_GET,'idPro');$prevpage = filter_input(INPUT_GET,'hd');
                                   include 'includes/tache.php';
                                   include 'includes/footer.php';
                            ?>    
                        </div> 
                <?php endif ?>
                 
            <?php else: ?>
                <div data-role="page" >
                     <?php
                            session_start();
                            $manager = new TacheManager($bdd);
                            
                            // Les projets 
                            if ( filter_input(INPUT_GET,'idProjet') != 0){
                                $_SESSION["categorie"] = 1;
                                $headerText = 'TacheProjet'; $prevlink='tacheSeance.php?idProjet='.filter_input(INPUT_GET,'idProjet');
                                $id = filter_input(INPUT_GET,'idProjet');
                                $listTache = $manager->getListTache($id,$_SESSION["id"],1);
                            }else if ( filter_input(INPUT_GET,'idProjet') == 0){
                                 // Liste des tâches/examens/stages en cours
                                 $headerText = 'Rappels'; $prevlink='accueil.php';
                                 $listTache = $manager->getListRappel($_SESSION["id"]);
                            }
                            
                            // Les examens 
                            if ( filter_input(INPUT_GET,'idExamen') != 0){
                                $_SESSION["categorie"] = 2;
                                $headerText = 'TacheExamen'; $prevlink='examen.php';
                                $id = filter_input(INPUT_GET,'idExamen');
                                $listTache = $manager->getListTache($id,$_SESSION["id"],2);
                            }
                            
                            //Les stages
                            if ( filter_input(INPUT_GET,'idStage') != 0){
             
                                $_SESSION["categorie"] = 3;
                                $headerText = 'TacheStage'; $prevlink='stage.php?idStage='.filter_input(INPUT_GET,'idStage').'&action=read';
                                $id = filter_input(INPUT_GET,'idStage');
                                $listTache = $manager->getListTache($id,$_SESSION["id"],3);
                            }
                            
                            include 'includes/header.php';
                            include 'includes/taches.php';
                      ?>
                </div>
            <?php endif ?>
        <?php endif ?>
        
        <script>
                  
                var isempty=0;
                var tache = {
                    "type":"0",
                    "id": "",
                    "idprojet": "",
                    "rappel": "" ,
                    "echeance": "" ,
                    "libelle": "" ,
                    "comment": "" 
                };
                
             	$("[name='type']").bind( "tap", function(e) {
                    console.log(e.target.innerHTML);
                    $("[href='#overlayPanel']").html(e.target.innerHTML); 
                    tache.type = e.target.id;
                });
                
                $("#valid").bind( "tap", function(e) {
                     $("[name='texte']").val(function(index,value){
                         if (value === ""){ 
                             isempty=1; 
                         }else{
                             if ( index === 0){
                                 tache.libelle = value;
                             }else if(index === 1){
                                 tache.echeance = value;
                             }else{
                                 tache.rappel = value;
                             }
                         }
                     }); 
                     console.log("isemty="+isempty);
                     if (isempty === 1 ){
                         $("#popupBasic").popup("open");
                         isempty=0;
                     }else{                         
                         tache.comment = $("#comment").val();
                         id = $("h1").attr('id');
                         tache.idprojet= $("h1").attr('name');
                         if ( id !== 0){
                             tache.id=id;
                         }
                         if ( tache.type === "0"){
                             // on prend l'ancien numéro du type
                             tache.type = $("[href='#overlayPanel']").attr('id');
                         }
                         
                         returnpage = $("#valid").attr('name');
                         window.location="Tache.php?act=c&task="+JSON.stringify(tache)+"&rtpage="+returnpage; 
                      }
                 });
                 
                 function delTache(id,idproj,rtpage){
                     if ( id===0){
                         $("#popupdel").popup("open");
                     }else{
                         window.location="Tache.php?id="+id+"&act=d&idPro="+idproj+"&rtpage="+rtpage;
                     }
                 }
        </script>
    </body>
</html>
